package com.qyi.activemq.queue.producer;

import com.qyi.activemq.queue.ActiveMQProperties;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.messaginghub.pooled.jms.JmsPoolConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jms.JmsPoolConnectionFactoryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.JmsTransactionManager;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;

/**
 * @author luqi
 * @Date 2021/3/11 14:18
 * @Description TODO
 */
@Configuration
public class MqProductConfig {

    public ActiveMQConnectionFactory activeMQConnectionFactory(ActiveMQProperties properties) {
        ActiveMQConnectionFactory activeMQConnectionFactory =
                new ActiveMQConnectionFactory(
                        properties.getUser(),
                        properties.getPassword(),
                        properties.getBrokerUrl());
        return activeMQConnectionFactory;
    }


    @Bean(name = "producerConnection")
    JmsPoolConnectionFactory jmsConnectionFactory(ActiveMQProperties properties) {
        ActiveMQConnectionFactory connectionFactory = (activeMQConnectionFactory(properties));
        JmsPoolConnectionFactory pooledConnectionFactory = (new JmsPoolConnectionFactoryFactory(properties.getPool())).createPooledConnectionFactory(connectionFactory);
        return pooledConnectionFactory;
    }


    @Bean
    public JmsTemplate jmsTemplate(@Qualifier("producerConnection") ConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        return jmsTemplate;
    }

    @Bean
    JmsTransactionManager jmsTransactionManager(@Qualifier("producerConnection") ConnectionFactory connectionFactory) {
        return new JmsTransactionManager(connectionFactory);
    }
}
